//
//  SmallIconView.m
//  TodatPlay
//
//  Created by dllo on 16/9/1.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "SmallIconView.h"
#import "FoundReuseableCollectionViewCell.h"
#import "TAMacro.h"
#import "UIImageView+WebCache.h"
#import "FoundFirstCategorydoc.h"
#import "CustomLayout.h"
#import "ClassifyViewController.h"
#import "FoundFirstReusableView.h"
#import "FoundViewController.h"


#define kCollectioViewReuse @"reuse"
#define kCollectionViewEmptyReuse @"emptyReuse"
@interface SmallIconView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate>
@property (nonatomic, strong) CustomLayout *layout;
@property (nonatomic, strong) UICollectionView *smallCollectionView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, copy) didSelectedIndexPath index;
@end

@implementation SmallIconView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}
#pragma mark -block-点击方法
- (void)selectIndexPath:(didSelectedIndexPath)index {
    self.index = index;
}


- (void)setUp {
    [self createCollectionView];
    [self createpageControl];
}
- (void)createCollectionView {
    // 创建自定义layout
    _layout = [[CustomLayout alloc] init];
    self.smallCollectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:_layout];
    self.smallCollectionView.pagingEnabled = YES;
    self.smallCollectionView.bounces = NO;
    self.smallCollectionView.showsHorizontalScrollIndicator = NO;
    self.smallCollectionView.delegate = self;
    self.smallCollectionView.dataSource = self;
    self.smallCollectionView.backgroundColor = [UIColor whiteColor];
    [self.smallCollectionView registerNib:[UINib nibWithNibName:@"FoundReuseableCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kCollectioViewReuse];
    [self addSubview:_smallCollectionView];
}
- (void)createpageControl {
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, (SCREEN_WIDTH -70) / 4.0, 10)];
    [self addSubview:_pageControl];
    _pageControl.numberOfPages = 2;
   
    self.pageControl.center = CGPointMake(SCREEN_WIDTH / 2.0, self.bounds.size.height - 20);
    _pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
    _pageControl.pageIndicatorTintColor = [UIColor colorWithWhite:0.800 alpha:1.000];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _categoryArr.count;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.smallCollectionView.frame = self.bounds;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

        FoundReuseableCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectioViewReuse forIndexPath:indexPath];
        //    cell.backgroundColor = [UIColor cyanColor];
//        NSLog(@"%@", self.categoryArr[indexPath.row].img);
        [cell.nameImageView sd_setImageWithURL:[NSURL URLWithString:self.categoryArr[indexPath.row].img] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
        }];
        cell.titleLabel.text = self.categoryArr[indexPath.row].name;
        return cell;

}
- (void)setCategoryArr:(NSMutableArray<FoundFirstCategorydoc *> *)categoryArr {
    _categoryArr = categoryArr;
    id first = categoryArr.firstObject;
    [categoryArr removeObjectAtIndex:0];
    [categoryArr addObject:first];
//    NSLog(@"%@", categoryArr);
    [self.smallCollectionView reloadData];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.pageControl.currentPage = scrollView.contentOffset.x / SCREEN_WIDTH;
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    self.index(indexPath);

    
    
//    id responder = self.nextResponder;
//    while (![responder isKindOfClass:[UINavigationController class]] && responder != nil) {
//        responder = [responder nextResponder];
//    }
//     UINavigationController *rootVC = (UINavigationController *)responder;
//    
//    id responderVC = self.nextResponder;
//    while (![responderVC isKindOfClass:[FoundViewController class]] && responderVC != nil) {
//        responderVC = [responderVC nextResponder];
//    }
//    FoundViewController *foundVC = (FoundViewController *)responder;
//    
//    foundVC.hidesBottomBarWhenPushed = YES;
//    ClassifyViewController *classifyVC = [[ClassifyViewController alloc] init];
//    [rootVC pushViewController:classifyVC animated:YES];
//    foundVC.hidesBottomBarWhenPushed = NO;
////    NSLog(@"点了第%ld张", indexPath.row);
    
    
}
@end
